
/**
    社区管理【模板】
    push mirthapi@965f6ae9-fef8-4e62-87df-6ad8f65636af
    @time:2021-4-6 12:21
    @author:kakake
    */
({
    data: {
        resultdata: {},
        dbConn: Program.data.dbConn,
        WorkId: Program.data.WorkId,
        requestBody: Program.data.requestBody,
        ShieldingWordModel: {
            model: {
                "Id": 0,
                "Word": "",
                "DelFlag": 0,
                "WorkId": 1
            },
            tablename: "Co_ShieldingWord",
            key: "Id",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: MirthAPI.ProgramData('workid') //机构ID
        },
        TopicModel: {
            model: {
                TopicId: 0,
                TopicName: "",
                TopicType: 0,
                TopicDesc: "",
                LookersCount: 0,
                WorkId: MirthAPI.ProgramData('workid'),//机构ID
                BackImg: "",
                DelFlag: 0,
                CreateTime: "yyyy-MM-dd HH:mm:ss"
            },
            tablename: "Co_Topic",
            key: "TopicId",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: MirthAPI.ProgramData('workid') //机构ID
        }
    },
    main: function (funname) {
        var self = this;
        self.data.dbConn = PublicFunction.setdbconn('10.0.4.5:1433', 'CloudCommunity', 'sa', 'Lobster@123');
        //查询
        if (funname == "shieldingword.getlist") {
            var page = MirthAPI.RequestQuery("page", 1);
            var perPage = MirthAPI.RequestQuery("limit", 10);
            var searchkey = MirthAPI.RequestQuery("searchkey", "");
            var datagrid = self.getshieldlist(page, perPage, searchkey);
            self.data.resultdata.data = datagrid.items;
            self.data.resultdata.count = datagrid.total;
        }
        //新增、修改
        else if (funname == "shieldingword.add") {
            var id = MirthAPI.RequestQuery("id", 0);
            var word = JSON.parse(self.data.requestBody);
            self.saveword(word);
        }
        //删除
        else if (funname == "shieldingword.delete") {
            var request = JSON.parse(self.data.requestBody);
            self.deleteword(request.Id);
        }
        else if (funname == "report.getlist") {
            var page = MirthAPI.RequestQuery("page", 1);
            var perPage = MirthAPI.RequestQuery("limit", 10);
            var reportDesc = MirthAPI.RequestQuery("reportDesc", "");
            var res = self.getreportlist(page, perPage, reportDesc);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        }
        //审核举报                     
        else if (funname == "report.auditreport") {
            var model = JSON.parse(self.data.requestBody);
            self.auditreport(model);
        }
        else if (funname == "topic.getlist") {
            var page = MirthAPI.RequestQuery("page", 1);
            var perPage = MirthAPI.RequestQuery("limit", 10);
            var searchkey = MirthAPI.RequestQuery("searchkey", "");
            var topictype = MirthAPI.RequestQuery("topicType", 10);
            var res = self.gettopiclist(page, perPage, searchkey, topictype);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        }
        //编辑话题                     
        else if (funname == "topic.save") {
            var model = JSON.parse(self.data.requestBody);
            self.savetopic(model);
        }
        else if (funname == "getdict") {
            var classId = MirthAPI.RequestQuery("classId", 0);
            var appid = MirthAPI.ProgramData('appid');
            self.data.resultdata.data = MirthAPI.AppFunction.GetAppBaseDict(appid, classId)
        } else if (funname == "getUploadUrl") {
            self.data.resultdata.data = {
                url: self.getUploadUrl(),
                ssotoken: MirthAPI.ProgramData('ssotoken')
            };
        }
        else if (funname == "dynamic.getlist") {
            var page = MirthAPI.RequestQuery("page", 1);
            var perPage = MirthAPI.RequestQuery("limit", 10);
            var searchkey = MirthAPI.RequestQuery("searchkey", "");
            var res = self.getdynamiclist(page, perPage, searchkey, topictype);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        }
        else if (funname == "dynamic.getattachlist") {
            var dynamicid = MirthAPI.RequestQuery("dynamicid", 0);
            var res = self.getdynamicattachlist(dynamicid);
            self.data.resultdata.data = res.data;
        }
        return self.data.resultdata;
    },
    getshieldlist: function (page, perPage, searchkey) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "Id";
        pageInfo.page = page;
        pageInfo.perPage = perPage;
        pageInfo.totalRecord = 100;

        self.data.ShieldingWordModel.where = "where WorkId=" + self.data.ShieldingWordModel.WorkId + " and Word like '%" + searchkey + "%' and DelFlag=0";
        self.data.ShieldingWordModel.pageinfo = pageInfo;

        var datagrid = {};
        datagrid.items = MirthAPI.OrmMySql.GetListModelPage(self.data.dbConn, self.data.ShieldingWordModel);
        datagrid.total = pageInfo.totalRecord;

        return datagrid;
    },
    saveword: function (word) {
        MirthAPI.TrackLog("saveword1", word);
        var self = this;
        var newword = {};
        newword.Id = word.Id;
        newword.Word = word.Word;
        newword.DelFlag = word.DelFlag;
        self.data.ShieldingWordModel.objval = newword;
        MirthAPI.TrackLog("saveword2", word);
        MirthAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.ShieldingWordModel);
    },
    deletebook: function (id) {
        var self = this;
        self.data.ShieldingWordModel.val = id;
        MirthAPI.OrmMySql.DeleteModel(self.data.dbConn, self.data.ShieldingWordModel);
    },
    getreportlist: function (page, perPage, reportDesc) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "ReportTime desc";
        pageInfo.page = page;
        pageInfo.perPage = perPage;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select a.*,b.NickName,b.UserImg,c.Title from Co_DynamicRelated a\
        inner join Co_Blogger b\
        on a.UserId=b.UserId\
        inner join Co_Dynamic c\
        on a.DynamicId=c.DynamicId\
        where a.IsReport=1";
        var dbparams = Packages.java.util.ArrayList();
        if ((reportDesc || "") != "") {
            strsql += " and a.ReportDesc Like ? ";
            dbparams.add('%' + reportDesc + '%');
        }
        var data = MirthAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    auditreport: function (model) {
        var self = this;
        var strsql = "update co_dynamicRelated set auditresult=?,AuditDesc=?,auditor=?,auditTime=GETDATE() where RelatedId=?;select @@IDENTITY";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(model.AuditResult);
        dbparams.add(model.AuditDesc);
        dbparams.add(MirthAPI.ProgramData('loginright').EmpName);
        dbparams.add(model.RelatedId);
        var data = MirthAPI.DbGetJsonObject(self.data.dbConn, strsql, dbparams);
    },
    gettopiclist: function (page, perPage, searchkey, topictype) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "LookersCount desc,CreateTime desc";
        pageInfo.page = page;
        pageInfo.perPage = perPage;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select * from Co_Topic Where WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(MirthAPI.ProgramData('workid'));
        if ((searchkey || "") != "") {
            strsql += " and TopicName Like ? ";
            dbparams.add('%' + searchkey + '%');
        }
        if (topictype > 0) {
            strsql += " and TopicType= ? ";
            dbparams.add(topictype);
        }
        var data = MirthAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    savetopic: function (topic) {
        var self = this;
        self.data.TopicModel.objval = topic;
        MirthAPI.TrackLog("savetopic", topic.CreateTime);
        MirthAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.TopicModel);
    },
    getUploadUrl: function () {
        var apiGateway = MirthAPI.apiGateway;
        return apiGateway + '/app/v1/oss/UploadLargeFile';
    },
    getdynamiclist: function (page, perPage, searchkey) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "CreateTime desc";
        pageInfo.page = page;
        pageInfo.perPage = perPage;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select a.*,b.NickName,c.TopicName\
        from Co_Dynamic a\
        inner join Co_Blogger b\
        on a.BloggerId=b.BloggerId\
        left join Co_Topic c\
        on a.TopicId=c.TopicId\
        where a.WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(MirthAPI.ProgramData('workid'));
        if ((searchkey || "") != "") {
            strsql += " and (b.NickName Like ? or a.Title like ? or a.Content like ?)";
            dbparams.add('%' + searchkey + '%');
            dbparams.add('%' + searchkey + '%');
            dbparams.add('%' + searchkey + '%');
        }
        var data = MirthAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    getdynamicattachlist(dynamicId) {
        var self = this;
        var strsql = "select Url from co_dynamicattach where dynamicid=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(dynamicId);
        return { data: MirthAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams) };
    },
    getCurrTime: function () {
        var self = this;
        return LobsterAPI.getCurrTime(self.data.dbConn, "yyyyMMddhhmmss", 0, 0, 0, 0);
    },
})


